package com.movie.ticket.sale.ticketassembly.alipay;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.movie.ticket.sale.moviecommon.common.Result;
import lombok.extern.slf4j.Slf4j;

/**
 * @author： Micsal
 * @create-time： 2020.12.16 20:53
 * @version： 1.0
 * @description： 支付宝退款
 */
@Slf4j
public class AlipayRefund {
    public static Result connect(RefundBean refundBean)  {
        AlipayClient alipayClient = new DefaultAlipayClient(
                PropertiesConfig.getKey("gatewayUrl"),//支付宝网关
                PropertiesConfig.getKey("app_id"),//appid
                PropertiesConfig.getKey("merchant_private_key"),//商户私钥
                "json",
                PropertiesConfig.getKey("charset"),//字符编码格式
                PropertiesConfig.getKey("alipay_public_key"),//支付宝公钥
                PropertiesConfig.getKey("sign_type")//签名方式
        );
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();

        alipayRequest.setBizContent(JSON.toJSONString(refundBean));

        AlipayTradeRefundResponse resp = null;
        try {
            resp = alipayClient.execute(alipayRequest);
        } catch (AlipayApiException e) {
            e.printStackTrace();
            log.info("退款失败：{}",e.getMessage());
            return Result.fail(426,e.getMessage());
        }

        String m1=resp.getMsg();
        String m2=resp.getSubMsg();
        String out_trade_no=resp.getOutTradeNo();
        String refund_fee=resp.getRefundFee();
        log.info("详细描述：{}",m1);
        log.info("详细信息：{}",m2);

        String message=m1+","+m2+","+out_trade_no+","+refund_fee;

        if (resp.isSuccess()) {
            log.info("退款成功");
            return Result.success("退款成功,"+message);
        } else {
            log.info("退款失败");
            return Result.fail(426,"退款失败:"+message);
        }
    }
}
